home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / cgazv3n4.zip / DOS4-DIR.ZIP / MEMORY.C < prev   
C/C++ Source or Header  |  1989-04-23  |  10KB  |  342 lines

  1. /**************************** MEMORY.C ********************************\
  2. |*                                                                    *|
  3. |* Displays in-memory tables and memory usage                         *|
  4. |*                                                                    *|
  5. |* (c) Copyright 1989, David Craig.  All rights reserved.             *|
  6. |* Permission is granted to incorporate these functions into a larger *|
  7. |* work and to distribute the resulting executable file.              *|
  8. |*                                                                    *|
  9. |* Usage: memory  [for PC/MS-DOS 4.0 and later]                       *|
  10. |*                                                                    *|
  11. |* Compilers: TurboC 2.0, MSC 5.1                                     *|
  12. |* Memory models: any                                                 *|
  13. \**********************************************************************/
  14.  
  15. /* must prevent MSC from word aligning contents of our structures */
  16. #pragma pack (1)
  17.  
  18. #include "local.h"
  19. #include "dos4.h"
  20. #if !defined(MK_FP)
  21. #define MK_FP(seg,ofs) (void far *) \
  22.                        ( ( ( (unsigned long) ((unsigned)(seg)) ) << 16) \
  23.                        |   ( (unsigned long) ((unsigned)(ofs)) )        )
  24. #endif
  25.  
  26. #define CHARACTER_DEVICE        0x8000
  27. #define IOCTL_SUPPORT           0x4000
  28. #define NON_IBM_FORMAT          0x2000          /* Output-until-busy (char) */
  29. #define OPEN_CLOSE_SUPPORT      0x0800
  30. #define GENERIC_IOCTL           0x0040
  31. #define IS_CLOCK                0x0008
  32. #define IS_NUL                  0x0004
  33. #define IS_STDOUT               0x0002          /* 32-bit sector numbers (blk) */
  34. #define IS_STDIN                0x0001
  35.  
  36. int     attr_bit_masks[] =
  37. {
  38.     IOCTL_SUPPORT, NON_IBM_FORMAT, OPEN_CLOSE_SUPPORT,
  39.     GENERIC_IOCTL, IS_CLOCK, IS_NUL, IS_STDOUT, IS_STDIN
  40. };
  41.  
  42. char    *char_attr_bit_texts[] =
  43. {
  44.     "(ioctl)", "(output-until-busy)", "(rmv)", "(generic ioctl)",
  45.     "(clock)", "(nul)", "(stdout)", "(stdin)"
  46. };
  47.  
  48. char    *block_attr_bit_texts[] =
  49. {
  50.     "(ioctl)", "(non-ibm)", "(rmv)", "(generic ioctl)",
  51.     "(clock)", "(nul)", "(32-bit sector numbers)", "(stdin)"
  52. };
  53.  
  54. void    get_pointers(void);
  55. void    process_mcbs(void);
  56. void    print_func_52h_table(void);
  57. void    list_devices(void);
  58. void    print_device_info(struct device_hdr far *device);
  59.  
  60. int     block_drivers = 0;
  61. int     character_drivers = 0;
  62. word    line_count;
  63. word    far *psp_ptr;
  64. word    far *first_mcb;
  65. struct  device_hdr far *current_device;
  66. struct  mcb far *mcbp;
  67. struct  mcb far *mcbt[1000];
  68. struct  func_52h far *func_52h;
  69.  
  70. int main(int argc, char **argv)
  71. {
  72.     printf("%s (c) 1989 David Craig, All rights reserved.\n",
  73.         argv[0]);
  74.     if(argc > 1)
  75.     {
  76.         fprintf(stderr, "%s requires no parameters.\n", argv[0]);
  77.         abort();
  78.     }
  79.     if(signal(SIGINT, SIG_IGN) == SIG_ERR)
  80.     {
  81.         cputs("\r\nCouldn't set SIGINT.\r\n");
  82.         abort();
  83.     }
  84.     get_pointers();
  85.     process_mcbs();
  86.     print_func_52h_table();
  87.     printf("\n");
  88.     list_devices();
  89.     return(0);
  90. }
  91.  
  92. void print_func_52h_table()
  93. {
  94.     line_count = 0;
  95.     printf("Dump of Function 52h table of pointers.\n\n");
  96.     printf("Pointer to first disk table = %Fp.\n", func_52h->disk_table);
  97.     printf("Pointer to System File Table (handle) = %Fp.\n",
  98.         func_52h->sft_ptr);
  99.     printf("Pointer to CLOCK$ device = %Fp.\n", func_52h->clock_ptr);
  100.     printf("Pointer to CON device = %Fp.\n", func_52h->con_ptr);
  101.     printf("Maximum sector size supported by DOS = 0x%04X.\n",
  102.         func_52h->max_sec);
  103.     printf("Pointer to Cache Control Block = %Fp.\n", func_52h->cache);
  104.     printf("Pointer to Current Directory Structure = %Fp.\n",
  105.         func_52h->drive_inf);
  106.     printf("Pointer to System File Table (FCB) = %Fp.\n", func_52h->fcb_ptr);
  107.     printf("Size of FCB System File Table = %d.\n", func_52h->size_fcb_table);
  108.     printf("Drive count = %d.\n", func_52h->drive_count);
  109.     printf("Last drive = %d.\n", func_52h->last_drive);
  110.     return;
  111. }
  112.  
  113. void process_mcbs()
  114. {
  115.     int     i;
  116.     int     j;
  117.     int     isgood;
  118.     int     table_index;
  119.     int     last_table_index;
  120.     int     last_mcb_config = 0;
  121.     byte    new[9];
  122.     byte    far *env_ptr;
  123.  
  124.     table_index = 0;
  125.     while(1)
  126.     {
  127.         mcbt[table_index++] = mcbp;
  128.         if(mcbp->control == 'Z')
  129.             break;
  130.         mcbp = MK_FP(FP_SEG(mcbp) + mcbp->size + 1, FP_OFF(mcbp));
  131.     }
  132.     mcbp = MK_FP(*first_mcb, 0);
  133.     last_table_index = table_index;
  134.     printf("\n\n");
  135.     printf("Addr  T  Own   Size   Name       Comments\n");
  136.     printf("____  _  ____  ____   ________   ____________________________\n");
  137.     line_count = 2;
  138.     while(1)
  139.     {
  140.         if(line_count > 23)
  141.         {
  142.             line_count = 0;
  143.         }
  144.         printf("%4X  %c  %4X  %4X   ", FP_SEG(mcbp),
  145.             mcbp->control, mcbp->owner, mcbp->size);
  146.         isgood = 1;
  147.         for(i = 0; i < 8; i++)
  148.         {
  149.             new[i] = mcbp->name[i];
  150.             if(isgood == 1)
  151.             {
  152.                 if(new[i] == '\0')
  153.                 {
  154.                     isgood = 2;
  155.                 }
  156.                 else
  157.                 {
  158.                     j = new[i];
  159.                     if(!isprint(j))
  160.                         isgood = 0;
  161.                 }
  162.             }
  163.         }
  164.         new[8] = '\0';
  165.         if(mcbp->owner == (FP_SEG(mcbp) + 1))
  166.         {
  167.             if(isgood)
  168.                 printf("%-8s   ", new);
  169.             else
  170.                 printf("           ");
  171.         }
  172.         else if(mcbp->owner == 0)
  173.         {
  174.             printf("(MS-DOS)   ");
  175.         }
  176.         else if(mcbp->owner == 8)
  177.         {
  178.             printf("(IBMBIO)   ");
  179.         }
  180.         else
  181.         {
  182.             printf("           ");
  183.         }
  184.         if(_osmajor < 4)
  185.         {
  186.             if(last_mcb_config)
  187.             {
  188.                 printf("(Shell)");
  189.             }
  190.             else
  191.             {
  192.                 printf("       ");
  193.             }
  194.         }
  195.         else
  196.         {
  197.             if(mcbp->owner == (FP_SEG(mcbp) + 1))
  198.             {
  199.                 psp_ptr = MK_FP( (FP_SEG(mcbp) + 1), 0x2c);
  200.                 if(*psp_ptr > FP_SEG(psp_ptr))
  201.                 {
  202.                     printf("(Shell)");
  203.                 }
  204.                 else
  205.                 {
  206.                     printf("       ");
  207.                 }
  208.             }
  209.         }
  210.         if(mcbp->owner == (FP_SEG(mcbp) + 1))
  211.         {
  212.             psp_ptr = MK_FP( (FP_SEG(mcbp) + 1), 0x2c);
  213.             env_ptr = MK_FP(*psp_ptr, 0);
  214.             psp_ptr = ( void far * ) env_ptr;
  215.             for(table_index = 0; table_index < last_table_index &&
  216.                 FP_SEG(mcbt[table_index]) != (FP_SEG(env_ptr) - 1);
  217.                 table_index++) ;
  218.             if(mcbt[table_index]->owner == (FP_SEG(mcbp) + 1))
  219.             {
  220.                 printf("   Environment at %04X.   ", FP_SEG(env_ptr));
  221.             }
  222.             else
  223.             {
  224.                 printf("   Environment at %04X (invalid).   ",
  225.                     FP_SEG(env_ptr));
  226.             }
  227.             if(*psp_ptr)
  228.             {
  229.                 for( ; *env_ptr; env_ptr++)
  230.                 {
  231.                     for( ; *env_ptr; env_ptr++) ;
  232.                 }
  233.                 env_ptr++;
  234.                 psp_ptr = ( void far * ) env_ptr;
  235.             }
  236.             else
  237.                 psp_ptr++;
  238.         }
  239.         printf("\n");
  240.         line_count++;
  241.         if(mcbp->owner == 8)
  242.             last_mcb_config = 1;
  243.         else
  244.             last_mcb_config = 0;
  245.         if(mcbp->control == 'Z')
  246.             break;
  247.         mcbp = MK_FP(FP_SEG(mcbp) + mcbp->size + 1, FP_OFF(mcbp));
  248.     }
  249.     if(line_count > 23)
  250.     {
  251.         line_count = 0;
  252.     }
  253.     printf("\nNumber of entries = %d.\n\n", last_table_index);
  254.     return;
  255. }
  256.  
  257. void get_pointers()
  258. {
  259.     union   REGS regs;
  260.     struct  SREGS sregs;
  261.  
  262.     segread(&sregs);
  263.     regs.x.ax = 0x5200;
  264.     (void) intdosx(®s, ®s, &sregs);
  265.     func_52h = MK_FP(sregs.es, regs.x.bx);
  266.     regs.x.bx -= 2;
  267.     first_mcb = MK_FP(sregs.es, regs.x.bx);
  268.     mcbp = MK_FP(*first_mcb, 0);
  269.     current_device = &func_52h->nul_dev;
  270.     return;
  271. }
  272.  
  273. void list_devices()
  274. {
  275.     line_count = 99;                    /* Force header to print */
  276.     while(FP_OFF(current_device) != ( word ) 0xffff)
  277.     {
  278.         if(line_count > 23)
  279.         {
  280.             line_count = 2;
  281.             printf("\n\n\n");
  282.             printf("Address   Flags Name/Units Strategy  Interrupt  Comments\n");
  283.             printf("--------- ----  ---------- --------- ---------  --------\n");
  284.         }
  285.         print_device_info(current_device);
  286.         current_device = current_device->next_dev;
  287.     }
  288.     if(++line_count > 21)
  289.     {
  290.         line_count = 0;
  291.     }
  292.     printf("\nSystem has %d character drivers, and %d block drivers.\n",
  293.         character_drivers, block_drivers);
  294.     return;
  295. }
  296.  
  297. void print_device_info(struct device_hdr far *device)
  298. {
  299.     char    device_string[9];
  300.     int     i;
  301.  
  302.     printf("%Fp %04X  ", device, device->attribute);
  303.     if(device->attribute & CHARACTER_DEVICE)
  304.     {
  305.         character_drivers++;
  306.         for(i = 0; i < 8; i++)
  307.             device_string[i] = device->name_unit[i];
  308.         device_string[8] = '\0';
  309.         printf("%s   ", device_string);
  310.     }
  311.     else
  312.     {
  313.         block_drivers++;
  314.         printf("%2u units   ", (word) device->name_unit[0]);
  315.     }
  316.     printf("%04X:%04X %04X:%04X  ", FP_SEG(device), device->strategy,
  317.         FP_SEG(device), device->intrupt);
  318.     if(!strcmp(device_string, "NUL     "))
  319.         printf("%u joined drives", (word) device->joined_drive_count);
  320.     if(device->attribute & ~CHARACTER_DEVICE)
  321.     {
  322.         printf("\n          ");
  323.         line_count++;
  324.     }
  325.     if(device->attribute & CHARACTER_DEVICE)
  326.     {
  327.         for(i = 0; i < 8; i++)
  328.             if(attr_bit_masks[i] & device->attribute)
  329.                 printf("%s ", char_attr_bit_texts[i]);
  330.     }
  331.     else
  332.     {
  333.         for(i = 0; i < 8; i++)
  334.             if(attr_bit_masks[i] & device->attribute)
  335.                 printf("%s ", block_attr_bit_texts[i]);
  336.     }
  337.     printf("\n");
  338.     line_count++;
  339.     return;
  340. }
  341.  
  342.